package com.zahb.qadx.ui.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.model.Curriculum;
import com.zahb.qadx.model.TrainAllBean;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.activity.CurriculumDetailsActivity;
import com.zahb.qadx.util.CompatHelper;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.ImageLoaderKt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class TrainRecommendAdapter extends RecyclerView.Adapter<TrainRecommendAdapter.TrainRecommendViewHodler> {
    private Context mContext;

    private List<TrainAllBean.RecommendListBean> dataBeanList;
    public TrainRecommendAdapter(Context mContext, List<TrainAllBean.RecommendListBean> dataBeanList) {
        this.mContext = mContext;
        this.dataBeanList = dataBeanList;
    }

    @NonNull
    @Override
    public TrainRecommendAdapter.TrainRecommendViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TrainRecommendAdapter.TrainRecommendViewHodler(LayoutInflater.from(mContext).inflate(R.layout.item_list_trainremmend, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull TrainRecommendAdapter.TrainRecommendViewHodler holder, int position) {

        String imgpic = dataBeanList.get(position).getImg();
        ImageLoaderKt.loadImage(holder.img_pic,imgpic);
        holder.tv_name1.setText(dataBeanList.get(position).getName());
        if (CompatHelper.isEmpty(dataBeanList.get(position).getSubName())){
            holder.tv_name.setText(dataBeanList.get(position).getName());
        }else {
            holder.tv_name.setText(dataBeanList.get(position).getSubName());
        }

        holder.tv_briefIntroduction.setText(dataBeanList.get(position).getType());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            tolearn(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataBeanList !=null ? dataBeanList.size() : 0;
    }
    static class TrainRecommendViewHodler extends RecyclerView.ViewHolder {
        private ImageView img_pic;
        private TextView tv_name1, tv_name,tv_briefIntroduction;

        public TrainRecommendViewHodler(@NonNull View itemView) {
            super(itemView);
            img_pic = itemView.findViewById(R.id.iv_train_icon);
            tv_name1 = itemView.findViewById(R.id.tv_classname1);
            tv_name = itemView.findViewById(R.id.tv_classname);
            tv_briefIntroduction = itemView.findViewById(R.id.tv_train_briefIntroduction);
        }
    }

    private Gson mGson = new Gson();
    private void tolearn( int position){
        if(BaseApplication.getContext().getDataLogin()==null){
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("courseId", dataBeanList.get(position).getTaskId());
        params.put("courseSnapshotId",dataBeanList.get(position).getTaskVersionId());
        params.put("relationshipId", dataBeanList.get(position).getClassUserTaskOnly());
        params.put("relationshipType", 2);
        params.put("rootOrgId", BaseApplication.getContext().getDataLogin().getCurrentOrg());
        params.put("userId", BaseApplication.getContext().getDataLogin().getUser().getId());

        String json = mGson.toJson(params);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        RetrofitService.getNetService()
                .toLearn(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        String courseUserId = response.getData();
                        Log.e("data===","data"+courseUserId);
                        Curriculum curriculum = new Curriculum(Integer.parseInt(dataBeanList.get(position).getTaskId()), dataBeanList.get(position).getName(), "",
                                dataBeanList.get(position).getImg(), dataBeanList.get(position).getTaskVersionId(),courseUserId );
                        curriculum.setRelationshipId(dataBeanList.get(position).getClassUserTaskOnly());
                        curriculum.setRelationshipType("2");
                        CurriculumDetailsActivity.actionStart(mContext, curriculum, CurriculumDetailsActivity.TYPE_FROM_HOME);
                    }
                }, throwable -> {
                    throwable.printStackTrace();

                });
    }
}
